<?php
/**
 * Copyright (c) 2006-2008, Julien PORTALIER
 * http://featherphp.googlecode.com/
 * 
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 */

/**
 * Generates tagclouds.
 */
class tagcloud
{
	/**
	 * Renders a tagcloud.
	 * 
	 * @return HTML
	 * @param $tags Associative Array: array($tag => $occurences)
	 * @param $base_link String[optional]
	 * @param $levels Integer[optional]
	 * @param $css_class String[optional]
	 */
	static function render(&$tags, $base_link='', $levels=9, $css_class='tag_')
	{
		$cloud = array();
		
		if (!empty($tags))
		{
			$min = min($tags);
			$max = max($tags);
			
			foreach($tags as $tag => $counter)
			{
				$curr_level = intval(($levels * ($counter - ($min - 1))) / (($max - ($min - 1)) + 1)) + 1;
				$swap_level = ($levels + 1) - $curr_level;
				
				$content = html::link(str_replace(' ', '&nbsp;', htmlspecialchars($tag)), $base_link.urlencode($tag), array(
					'title' => sprintf(tn('One occurence', '%d occurences', $counter, 'global.tags'), $counter),
					'rel' => 'tag'
				));
				$cloud[] = html::content_tag('li', $content, array('class' => $css_class.$swap_level));
			}
		}
		return '<ul class="tagcloud">'.implode(' ', $cloud).'</ul>';
	}
}
?>
